import axios from "axios";
import Vue from "vue";
import { Toast } from "vant";

const active = "pro";

//后端服务器地址
const ipAddress = {
  local: "/api", //本地环境
  dev: "http://127.0.0.1:8099", //开发环境
  pro: "http://47.242.243.166:8099" //生产环境
};

const baseURL = ipAddress[active];

const $axios = axios.create({
  // 设置超时时间
  // timeout: 10000,
  // 基础url，会在请求url中自动添加前置链接
  // baseURL: process.env.VUE_APP_BASE_API
});
Vue.prototype.$http = axios; // 并发请求
// 在全局请求和响应拦截器中添加请求状态
let loading = null;

// 请求拦截器
$axios.interceptors.request.use(
  config => {
    loading = Toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true,
      message: "loading"
    });
    let userInfo = sessionStorage.getItem("token");
    if (userInfo && userInfo.length > 0) {
      const token = userInfo;
      if (token) {
        config.headers.Authorization = "Bearer " + token; // 请求头部添加token
      }
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
// 响应拦截器
$axios.interceptors.response.use(
  response => {
    if (loading) {
      Toast.clear();
    }
    const code = response.status;
    //if ((code >= 200 && code < 300) || code === 304) {
      return Promise.resolve(response.data);
   // } else {
     // return Promise.reject(response);
   // }
  },
  error => {
    if (loading) {
      Toast.clear();
    }
    let msg="";
    let stateCode="";
    if (error.response) {
      switch (error.response.status) {
        case 401:
         msg="401:没有请求权限";
          break;
        case 404:
          msg="404:网络请求不存在";
          break;
          case 504:
            msg="504:网关超时";
            break;
        // case 501:
        //   msg="501:"+error.response.statusText;
        // case 500:
        //   msg="500:"+error.response.statusText;
        //   break;
        default:
         msg=error.response.status+":"+error.response.statusText;
      }
      stateCode=error.response.status;
    } else {
      // // 请求超时或者网络有问题
      // if (error.message.includes("timeout")) {
      //   Toast.fail("网络请求超时！");
      // } else {
      //   Toast.fail("请求失败！");
      // }
      msg="系统服务加载超时";
    }
   // return Promise.reject(error);
   return Promise.resolve({stateCode:stateCode,msg:msg});
  }
);

// get，post请求方法
export default {
  post(url, data, headers) {
    debugger;
    url = baseURL + url;
    return $axios({
      method: "post",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" },
      data: data
    });
  },
  postBlob(url, data, headers) {
    debugger;
    url = baseURL + url;
    return $axios({
      method: "post",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" },
      data: data,
      responseType: "blob"
    });
  },
  getBlob(url, data, headers) {
    url = baseURL + url;
    return $axios({
      method: "get",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" },
      data: data,
      responseType: "blob"
    });
  },
  get(url, params, headers) {
    //    /abcdefg ? id=123456  带 问号拼接的 url
    url = baseURL + url;
    return $axios({
      method: "get",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" },
      params
    });
  },
  _get(url, headers) {
    //    /abcdefg/123456    不带问号拼接的 url
    url = baseURL + url;
    return $axios({
      method: "get",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" }
    });
  },
  delete(url, headers) {
    url = baseURL + url;
    return $axios({
      method: "DELETE",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" }
    });
  },
  put(url, data, headers) {
    url = baseURL + url;
    return $axios({
      method: "PUT",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" },
      data
    });
  },
  _put(url, headers) {
    url = baseURL + url;
    return $axios({
      method: "PUT",
      url,
      headers: headers
        ? headers
        : { "content-type": "application/json;charset=UTF-8" }
    });
  }
};
